package com.kaiwukj.android.ufamily.mvp.presenter;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.contract.ReportContract;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.ReportRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.ReportTypeResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

/**
 * @author Charles
 * @date 2019/8/22.
 * description：Kaiwu
 */
@ActivityScope
public class ReportPresenter extends BasePresenter<ReportContract.Model, ReportContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public ReportPresenter(ReportContract.Model model, ReportContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 获取举报类型
     */
    public void requestReportType() {
        mModel.requestReportType()
                .subscribeOn(Schedulers.io())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.handleResult())
                .subscribe(new ApiCallBack<List<ReportTypeResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<ReportTypeResult> result) {
                        mRootView.getReportType(result);
                    }
                });
    }

    /**
     * 提交举报
     */
    public void postReport(ReportRequest reportRequest) {
        mModel.postReport(reportRequest)
                .subscribeOn(Schedulers.io())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.handleCode())
                .subscribe(new ApiCallBack<Integer>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.reportSuccess();
                    }
                });
    }
}
